package de.ur.iw.wiki.gui;

import static de.ur.iw.wiki.gui.WikiGUIUtil.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.HashMap;

import javax.swing.JCheckBox;
import javax.swing.JTextField;

import de.ur.iw.interfaces.OnSettingsChangedListener;
import de.ur.iw.wiki.Settings;

public class WikiGUIActionListener implements ActionListener, ItemListener {

	private OnSettingsChangedListener listener;
	private WikiGUI wikigui;

	public WikiGUIActionListener(WikiGUI main) {
		wikigui = main;
	}

	@Override
	public void actionPerformed(ActionEvent ev) {
		String command = ev.getActionCommand();
		if (command.equals(BUTTON_RANDOMWALK)) {
			HashMap<String,JTextField> textFields = wikigui.getTextFields();
			
			notifyListener(Settings.KEY_DATABASE_LOCATION, textFields.get(DATABASE_LOCATION).getText());
			notifyListener(Settings.KEY_DATABASE_NAME, textFields.get(DATABASE_NAME).getText());
			notifyListener(Settings.KEY_DATABASE_TABLENAME, textFields.get(DATABASE_TABLENAME).getText());
			notifyListener(Settings.KEY_DATABASE_USERNAME, textFields.get(DATABASE_USERNAME).getText());
			notifyListener(Settings.KEY_DATABASE_PASSWORD, textFields.get(DATABASE_PASSWORD).getText());
			notifyListener(Settings.KEY_CATEGORIES_WEIGHT, textFields.get(LABEL_CATEGORIES).getText());
			notifyListener(Settings.KEY_MAX_PATHS_VALUE, textFields.get(LABEL_LIMIT_PATHS).getText());
			notifyListener(Settings.KEY_START_PAGE, textFields.get(LABEL_START_PAGE).getText());
			notifyListener(Settings.KEY_RESULT_LOCATION, textFields.get(LABEL_HOST_URL).getText());
			
			listener.onSettingsChanged(Settings.KEY_RANDOMWALK_START, true);
			return;
		}else if(command.equals(BUTTON_WIZARD)){
			wikigui.startWizard();
		}else if(command.equals(BUTTON_RESULTS)){
			HashMap<String,JTextField> textFields = wikigui.getTextFields();
			
			notifyListener(Settings.KEY_RESULT_LOCATION, textFields.get(LABEL_HOST_URL).getText());
			notifyListener(Settings.KEY_OPEN_RESULTS, null);
		}
	}

	@Override
	public void itemStateChanged(ItemEvent iv) {
		boolean selected;
		if (iv.getStateChange() == ItemEvent.DESELECTED) {
			selected = false;
		} else {
			selected = true;
		}
		String label = ((JCheckBox) iv.getSource()).getText();

		if (label.equals(LABEL_DEAD_END)) {
			listener.onSettingsChanged(Settings.KEY_DEAD_END,
					selected);
		} else if (label.equals(LABEL_FILES)) {
			listener.onSettingsChanged(Settings.KEY_FILES,
					selected);
		} else if (label.equals(LABEL_LIMITED_TO_CATEGORYS)) {
			listener.onSettingsChanged(
					Settings.KEY_LIMIT_TO_CATEGORIES, selected);
		} else if (label.equals(LABEL_PREVENT_LOOPS)) {
			listener.onSettingsChanged(
					Settings.KEY_PREVENT_LOOPS, selected);
		}
	}

	public void setOnSettingsChangedListener(OnSettingsChangedListener listener) {
		this.listener = listener;
	}

	public void removeOnSettingsChangedListener() {
		this.listener = null;
	}
	
	public void notifyListener(int key, String value){
		if(value != ""){
			if(key == Settings.KEY_CATEGORIES_WEIGHT){
				try{
					listener.onSettingsChanged(key, Double.parseDouble(value));
				}catch(NumberFormatException e){}
			}else if(key == Settings.KEY_MAX_PATHS_VALUE){
				try{
					listener.onSettingsChanged(key, Integer.parseInt(value));
				}catch(NumberFormatException e){}
			}else{
				listener.onSettingsChanged(key, value);
			}
		}
	}

}
